package com.google.code.opengw.server.deployment;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.google.code.opengw.server.Server;
import com.google.code.opengw.server.support.FileUtils;
import com.google.code.opengw.support.OpenGWLogger;

public class Deployment {

	private String publishName;
	private FileHolder publishXML;
	private FileHolder[] fileList;

	public Deployment(File publishDirectory) throws FileNotFoundException,
			IOException {
		this.publishName = publishDirectory.getName();
		List<FileHolder> fileHolderList = new ArrayList<FileHolder>();
		File[] children = publishDirectory.listFiles();
		OpenGWLogger.addInfo(this.getClass(), "Copying files : " + publishName);
		for (File child : children) {
			if (child.isDirectory()) {
				fileHolderList.add(new FileHolder(true, child.getName(), null));
				searchDirectory(child, child.getName(), fileHolderList);
			} else {
				FileHolder fileHolder = new FileHolder(false, child.getName(),
						FileUtils
								.readDataFromInputStream(new FileInputStream(
										child)));
				fileHolderList.add(fileHolder);
				OpenGWLogger.addInfo(this.getClass(), "File copied : " + fileHolder.getRelativePath());
				if (child.getName().equals(Server.PUBLISH_FILE_NAME))
					publishXML = fileHolder;
			}

		}

		fileList = fileHolderList
				.toArray(new FileHolder[fileHolderList.size()]);
		OpenGWLogger.addInfo(this.getClass(), "Copying files are completed : " + publishName);

	}

	private void searchDirectory(File file, String parentPath,
			List<FileHolder> fileHolderList) throws FileNotFoundException,
			IOException {
		
		File[] children = file.listFiles();
		for (File child : children){
			if(child.isDirectory()) {
				searchDirectory(child, new StringBuilder().append(parentPath)
					.append(System.getProperty("file.separator"))
					.append(child.getName())
					.toString(), fileHolderList);
			} else {
				FileHolder fileHolder = new FileHolder(false, new StringBuilder()
					.append(parentPath)
					.append(System.getProperty("file.separator"))
					.append(child.getName()).toString(), FileUtils
						.readDataFromInputStream(new FileInputStream(child)));
				fileHolderList.add(fileHolder);
				OpenGWLogger.addInfo(this.getClass(), "File copied : " + fileHolder.getRelativePath());
			}
		}
	}

	public InputStream getPublishXMLStream() {
		return new ByteArrayInputStream(getPublishXML().getContent());
	}

	public boolean checkResource(String resourceURL) {
		for (FileHolder holder : getFileList()) {
			if (holder.getRelativePath().equals(resourceURL))
				return true;
		}

		return false;
	}

	public String getPublishName() {
		return publishName;
	}

	public void setPublishName(String publishName) {
		this.publishName = publishName;
	}

	public FileHolder getPublishXML() {
		return publishXML;
	}

	public void setPublishXML(FileHolder publishXML) {
		this.publishXML = publishXML;
	}

	public FileHolder[] getFileList() {
		return fileList;
	}

	public void setFileList(FileHolder[] fileList) {
		this.fileList = fileList;
	}
	
	

}
